home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsbitops.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  23.3 KB  |  887 lines

  1. /* Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsbitops.c,v 1.2 2000/09/19 19:00:25 lpd Exp $ */
  20. /* Bitmap filling, copying, and transforming operations */
  21. #include "stdio_.h"
  22. #include "memory_.h"
  23. #include "gdebug.h"
  24. #include "gserror.h"
  25. #include "gserrors.h"
  26. #include "gstypes.h"
  27. #include "gsbittab.h"
  28. #include "gxbitops.h"
  29.  
  30. /*
  31.  * Define a compile-time option to reverse nibble order in alpha maps.
  32.  * Note that this does not reverse bit order within nibbles.
  33.  * This option is here for a very specialized purpose and does not
  34.  * interact well with the rest of the code.
  35.  */
  36. #ifndef ALPHA_LSB_FIRST
  37. #  define ALPHA_LSB_FIRST 0
  38. #endif
  39.  
  40. /* ---------------- Bit-oriented operations ---------------- */
  41.  
  42. /* Define masks for little-endian operation. */
  43. /* masks[i] has the first i bits off and the rest on. */
  44. #if !arch_is_big_endian
  45. const bits16 mono_copy_masks[17] = {
  46.     0xffff, 0xff7f, 0xff3f, 0xff1f,
  47.     0xff0f, 0xff07, 0xff03, 0xff01,
  48.     0xff00, 0x7f00, 0x3f00, 0x1f00,
  49.     0x0f00, 0x0700, 0x0300, 0x0100,
  50.     0x0000
  51. };
  52. const bits32 mono_fill_masks[33] = {
  53. #define mask(n)\
  54.   ((~0xff | (0xff >> (n & 7))) << (n & -8))
  55.     mask( 0),mask( 1),mask( 2),mask( 3),mask( 4),mask( 5),mask( 6),mask( 7),
  56.     mask( 8),mask( 9),mask(10),mask(11),mask(12),mask(13),mask(14),mask(15),
  57.     mask(16),mask(17),mask(18),mask(19),mask(20),mask(21),mask(22),mask(23),
  58.     mask(24),mask(25),mask(26),mask(27),mask(28),mask(29),mask(30),mask(31),
  59.     0
  60. #undef mask
  61. };
  62. #endif
  63.  
  64. /* Fill a rectangle of bits with an 8x1 pattern. */
  65. /* The pattern argument must consist of the pattern in every byte, */
  66. /* e.g., if the desired pattern is 0xaa, the pattern argument must */
  67. /* have the value 0xaaaa (if ints are short) or 0xaaaaaaaa. */
  68. #undef chunk
  69. #define chunk mono_fill_chunk
  70. #undef mono_masks
  71. #define mono_masks mono_fill_masks
  72. void
  73. bits_fill_rectangle(byte * dest, int dest_bit, uint draster,
  74.             mono_fill_chunk pattern, int width_bits, int height)
  75. {
  76.     uint bit;
  77.     chunk right_mask;
  78.     int line_count = height;
  79.     chunk *ptr;
  80.     int last_bit;
  81.  
  82. #define FOR_EACH_LINE(stat)\
  83.     do { stat } while ( inc_ptr(ptr, draster), --line_count )
  84.  
  85.     dest += (dest_bit >> 3) & -chunk_align_bytes;
  86.     ptr = (chunk *) dest;
  87.     bit = dest_bit & chunk_align_bit_mask;
  88.     last_bit = width_bits + bit - (chunk_bits + 1);
  89.  
  90.     if (last_bit < 0) {        /* <=1 chunk */
  91.     set_mono_thin_mask(right_mask, width_bits, bit);
  92.     switch ((byte) pattern) {
  93.         case 0:
  94.         FOR_EACH_LINE(*ptr &= ~right_mask;
  95.             );
  96.         break;
  97.         case 0xff:
  98.         FOR_EACH_LINE(*ptr |= right_mask;
  99.             );
  100.         break;
  101.         default:
  102.         FOR_EACH_LINE(
  103.                *ptr = (*ptr & ~right_mask) | (pattern & right_mask);
  104.             );
  105.     }
  106.     } else {
  107.     chunk mask;
  108.     int last = last_bit >> chunk_log2_bits;
  109.  
  110.     set_mono_left_mask(mask, bit);
  111.     set_mono_right_mask(right_mask, (last_bit & chunk_bit_mask) + 1);
  112.     switch (last) {
  113.         case 0:        /* 2 chunks */
  114.         switch ((byte) pattern) {
  115.             case 0:
  116.             FOR_EACH_LINE(*ptr &= ~mask;
  117.                       ptr[1] &= ~right_mask;
  118.                 );
  119.             break;
  120.             case 0xff:
  121.             FOR_EACH_LINE(*ptr |= mask;
  122.                       ptr[1] |= right_mask;
  123.                 );
  124.             break;
  125.             default:
  126.             FOR_EACH_LINE(
  127.                    *ptr = (*ptr & ~mask) | (pattern & mask);
  128.                      ptr[1] = (ptr[1] & ~right_mask) | (pattern & right_mask);
  129.                 );
  130.         }
  131.         break;
  132.         case 1:        /* 3 chunks */
  133.         switch ((byte) pattern) {
  134.             case 0:
  135.             FOR_EACH_LINE(
  136.                      *ptr &= ~mask;
  137.                      ptr[1] = 0;
  138.                      ptr[2] &= ~right_mask;
  139.                 );
  140.             break;
  141.             case 0xff:
  142.             FOR_EACH_LINE(
  143.                      *ptr |= mask;
  144.                      ptr[1] = ~(chunk) 0;
  145.                      ptr[2] |= right_mask;
  146.                 );
  147.             break;
  148.             default:
  149.             FOR_EACH_LINE(
  150.                    *ptr = (*ptr & ~mask) | (pattern & mask);
  151.                      ptr[1] = pattern;
  152.                      ptr[2] = (ptr[2] & ~right_mask) | (pattern & right_mask);
  153.                 );
  154.         }
  155.         break;
  156.         default:{        /* >3 chunks */
  157.             uint byte_count = (last_bit >> 3) & -chunk_bytes;
  158.  
  159.             switch ((byte) pattern) {
  160.             case 0:
  161.                 FOR_EACH_LINE(
  162.                          *ptr &= ~mask;
  163.                          memset(ptr + 1, 0, byte_count);
  164.                          ptr[last + 1] &= ~right_mask;
  165.                 );
  166.                 break;
  167.             case 0xff:
  168.                 FOR_EACH_LINE(
  169.                          *ptr |= mask;
  170.                       memset(ptr + 1, 0xff, byte_count);
  171.                          ptr[last + 1] |= right_mask;
  172.                 );
  173.                 break;
  174.             default:
  175.                 FOR_EACH_LINE(
  176.                    *ptr = (*ptr & ~mask) | (pattern & mask);
  177.                 memset(ptr + 1, (byte) pattern, byte_count);
  178.                          ptr[last + 1] =
  179.                          (ptr[last + 1] & ~right_mask) |
  180.                          (pattern & right_mask);
  181.                 );
  182.             }
  183.         }
  184.     }
  185.     }
  186. #undef FOR_EACH_LINE
  187. }
  188.  
  189. /* Replicate a bitmap horizontally in place. */
  190. void
  191. bits_replicate_horizontally(byte * data, uint width, uint height,
  192.          uint raster, uint replicated_width, uint replicated_raster)
  193. {
  194.     /* The current algorithm is extremely inefficient! */
  195.     const byte *orig_row = data + (height - 1) * raster;
  196.     byte *tile_row = data + (height - 1) * replicated_raster;
  197.     uint y;
  198.  
  199.     if (!(width & 7)) {
  200.     uint src_bytes = width >> 3;
  201.     uint dest_bytes = replicated_width >> 3;
  202.  
  203.     for (y = height; y-- > 0;
  204.          orig_row -= raster, tile_row -= replicated_raster
  205.          ) {
  206.         uint move = src_bytes;
  207.         const byte *from = orig_row;
  208.         byte *to = tile_row + dest_bytes - src_bytes;
  209.  
  210.         memmove(to, from, move);
  211.         while (to - tile_row >= move) {
  212.         from = to;
  213.         to -= move;
  214.         memmove(to, from, move);
  215.         move <<= 1;
  216.         }
  217.         if (to != tile_row)
  218.         memmove(tile_row, to, to - tile_row);
  219.     }
  220.     } else {
  221.     /*
  222.      * This algorithm is inefficient, but probably not worth improving.
  223.      */
  224.     uint bit_count = width & -width;  /* lowest bit: 1, 2, or 4 */
  225.     uint left_mask = (0xff00 >> bit_count) & 0xff;
  226.  
  227.     for (y = height; y-- > 0;
  228.          orig_row -= raster, tile_row -= replicated_raster
  229.          ) {
  230.         uint sx;
  231.  
  232.         for (sx = width; sx > 0;) {
  233.         uint bits, dx;
  234.  
  235.         sx -= bit_count;
  236.         bits = (orig_row[sx >> 3] << (sx & 7)) & left_mask;
  237.         for (dx = sx + replicated_width; dx >= width;) {
  238.             byte *dp;
  239.             int dbit;
  240.  
  241.             dx -= width;
  242.             dbit = dx & 7;
  243.             dp = tile_row + (dx >> 3);
  244.             *dp = (*dp & ~(left_mask >> dbit)) | (bits >> dbit);
  245.         }
  246.         }
  247.     }
  248.     }
  249. }
  250.  
  251. /* Replicate a bitmap vertically in place. */
  252. void
  253. bits_replicate_vertically(byte * data, uint height, uint raster,
  254.               uint replicated_height)
  255. {
  256.     byte *dest = data;
  257.     uint h = replicated_height;
  258.     uint size = raster * height;
  259.  
  260.     while (h > height) {
  261.     memcpy(dest + size, dest, size);
  262.     dest += size;
  263.     h -= height;
  264.     }
  265. }
  266.  
  267. /* Find the bounding box of a bitmap. */
  268. /* Assume bits beyond the width are zero. */
  269. void
  270. bits_bounding_box(const byte * data, uint height, uint raster,
  271.           gs_int_rect * pbox)
  272. {
  273.     register const ulong *lp;
  274.     static const byte first_1[16] = {
  275.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
  276.     };
  277.     static const byte last_1[16] = {
  278.     0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4
  279.     };
  280.  
  281.     /* Count trailing blank rows. */
  282.     /* Since the raster is a multiple of sizeof(long), */
  283.     /* we don't need to scan by bytes, only by longs. */
  284.  
  285.     lp = (const ulong *)(data + raster * height);
  286.     while ((const byte *)lp > data && !lp[-1])
  287.     --lp;
  288.     if ((const byte *)lp == data) {
  289.     pbox->p.x = pbox->q.x = pbox->p.y = pbox->q.y = 0;
  290.     return;
  291.     }
  292.     pbox->q.y = height = ((const byte *)lp - data + raster - 1) / raster;
  293.  
  294.     /* Count leading blank rows. */
  295.  
  296.     lp = (const ulong *)data;
  297.     while (!*lp)
  298.     ++lp;
  299.     {
  300.     uint n = ((const byte *)lp - data) / raster;
  301.  
  302.     pbox->p.y = n;
  303.     if (n)
  304.         height -= n, data += n * raster;
  305.     }
  306.  
  307.     /* Find the left and right edges. */
  308.     /* We know that the first and last rows are non-blank. */
  309.  
  310.     {
  311.     uint raster_longs = raster >> arch_log2_sizeof_long;
  312.     uint left = raster_longs - 1, right = 0;
  313.     ulong llong = 0, rlong = 0;
  314.     const byte *q;
  315.     uint h, n;
  316.  
  317.     for (q = data, h = height; h-- > 0; q += raster) {    /* Work from the left edge by longs. */
  318.         for (lp = (const ulong *)q, n = 0;
  319.          n < left && !*lp; lp++, n++
  320.         );
  321.         if (n < left)
  322.         left = n, llong = *lp;
  323.         else
  324.         llong |= *lp;
  325.         /* Work from the right edge by longs. */
  326.         for (lp = (const ulong *)(q + raster - sizeof(long)),
  327.          n = raster_longs - 1;
  328.  
  329.          n > right && !*lp; lp--, n--
  330.         );
  331.         if (n > right)
  332.         right = n, rlong = *lp;
  333.         else
  334.         rlong |= *lp;
  335.     }
  336.  
  337.     /* Do binary subdivision on edge longs.  We assume that */
  338.     /* sizeof(long) = 4 or 8. */
  339. #if arch_sizeof_long > 8
  340.     Error_longs_are_too_large();
  341. #endif
  342.  
  343. #if arch_is_big_endian
  344. #  define last_bits(n) ((1L << (n)) - 1)
  345. #  define shift_out_last(x,n) ((x) >>= (n))
  346. #  define right_justify_last(x,n) DO_NOTHING
  347. #else
  348. #  define last_bits(n) (-1L << ((arch_sizeof_long * 8) - (n)))
  349. #  define shift_out_last(x,n) ((x) <<= (n))
  350. #  define right_justify_last(x,n) (x) >>= ((arch_sizeof_long * 8) - (n))
  351. #endif
  352.  
  353.     left <<= arch_log2_sizeof_long + 3;
  354. #if arch_sizeof_long == 8
  355.     if (llong & ~last_bits(32))
  356.         shift_out_last(llong, 32);
  357.     else
  358.         left += 32;
  359. #endif
  360.     if (llong & ~last_bits(16))
  361.         shift_out_last(llong, 16);
  362.     else
  363.         left += 16;
  364.     if (llong & ~last_bits(8))
  365.         shift_out_last(llong, 8);
  366.     else
  367.         left += 8;
  368.     right_justify_last(llong, 8);
  369.     if (llong & 0xf0)
  370.         left += first_1[(byte) llong >> 4];
  371.     else
  372.         left += first_1[(byte) llong] + 4;
  373.  
  374.     right <<= arch_log2_sizeof_long + 3;
  375. #if arch_sizeof_long == 8
  376.     if (!(rlong & last_bits(32)))
  377.         shift_out_last(rlong, 32);
  378.     else
  379.         right += 32;
  380. #endif
  381.     if (!(rlong & last_bits(16)))
  382.         shift_out_last(rlong, 16);
  383.     else
  384.         right += 16;
  385.     if (!(rlong & last_bits(8)))
  386.         shift_out_last(rlong, 8);
  387.     else
  388.         right += 8;
  389.     right_justify_last(rlong, 8);
  390.     if (!(rlong & 0xf))
  391.         right += last_1[(byte) rlong >> 4];
  392.     else
  393.         right += last_1[(uint) rlong & 0xf] + 4;
  394.  
  395.     pbox->p.x = left;
  396.     pbox->q.x = right;
  397.     }
  398. }
  399.  
  400. /* Count the number of 1-bits in a half-byte. */
  401. static const byte half_byte_1s[16] = {
  402.     0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
  403. };
  404.  
  405. /* Count the number of trailing 1s in an up-to-5-bit value, -1. */
  406. static const byte bits5_trailing_1s[32] = {
  407.     0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3,
  408.     0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 4
  409. };
  410.  
  411. /* Count the number of leading 1s in an up-to-5-bit value, -1. */
  412. static const byte bits5_leading_1s[32] = {
  413.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  414.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4
  415. };
  416.  
  417. /*
  418.  * Compress a value between 0 and 2^M to a value between 0 and 2^N-1.
  419.  * Possible values of M are 1, 2, 3, or 4; of N are 1, 2, and 4.
  420.  * The name of the table is compress_count_M_N.
  421.  * As noted below, we require that N <= M.
  422.  */
  423. static const byte compress_1_1[3] = {
  424.     0, 1, 1
  425. };
  426. static const byte compress_2_1[5] = {
  427.     0, 0, 1, 1, 1
  428. };
  429. static const byte compress_2_2[5] = {
  430.     0, 1, 2, 2, 3
  431. };
  432. static const byte compress_3_1[9] = {
  433.     0, 0, 0, 0, 1, 1, 1, 1, 1
  434. };
  435. static const byte compress_3_2[9] = {
  436.     0, 0, 1, 1, 2, 2, 2, 3, 3
  437. };
  438. static const byte compress_4_1[17] = {
  439.     0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  440. };
  441. static const byte compress_4_2[17] = {
  442.     0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3
  443. };
  444. static const byte compress_4_4[17] = {
  445.     0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15
  446. };
  447.  
  448. /* The table of tables is indexed by log2(N) and then by M-1. */
  449. static const byte *const compress_tables[4][4] = {
  450.     {compress_1_1, compress_2_1, compress_3_1, compress_4_1},
  451.     {0, compress_2_2, compress_3_2, compress_4_2},
  452.     {0, 0, 0, compress_4_4}
  453. };
  454.  
  455. /*
  456.  * Compress an XxY-oversampled bitmap to Nx1 by counting 1-bits.  The X and
  457.  * Y oversampling factors are 1, 2, or 4, but may be different.  N, the
  458.  * resulting number of (alpha) bits per pixel, may be 1, 2, or 4; we allow
  459.  * compression in place, in which case N must not exceed the X oversampling
  460.  * factor.  Width and height are the source dimensions, and hence reflect
  461.  * the oversampling; both are multiples of the relevant scale factor.  The
  462.  * same is true for srcx.
  463.  */
  464. void
  465. bits_compress_scaled(const byte * src, int srcx, uint width, uint height,
  466.              uint sraster, byte * dest, uint draster,
  467.          const gs_log2_scale_point * plog2_scale, int log2_out_bits)
  468. {
  469.     int log2_x = plog2_scale->x, log2_y = plog2_scale->y;
  470.     int xscale = 1 << log2_x;
  471.     int yscale = 1 << log2_y;
  472.     int out_bits = 1 << log2_out_bits;
  473.     /*
  474.      * The following two initializations are only needed (and the variables
  475.      * are only used) if out_bits <= xscale.  We do them in all cases only
  476.      * to suppress bogus "possibly uninitialized variable" warnings from
  477.      * certain compilers.
  478.      */
  479.     int input_byte_out_bits = out_bits << (3 - log2_x);
  480.     byte input_byte_out_mask = (1 << input_byte_out_bits) - 1;
  481.     const byte *table =
  482.     compress_tables[log2_out_bits][log2_x + log2_y - 1];
  483.     uint sskip = sraster << log2_y;
  484.     uint dwidth = (width >> log2_x) << log2_out_bits;
  485.     uint dskip = draster - ((dwidth + 7) >> 3);
  486.     uint mask = (1 << xscale) - 1;
  487.     uint count_max = 1 << (log2_x + log2_y);
  488.  
  489.     /*
  490.      * For right now, we don't attempt to take advantage of the fact
  491.      * that the input is aligned.
  492.      */
  493.     const byte *srow = src + (srcx >> 3);
  494.     int in_shift_initial = 8 - xscale - (srcx & 7);
  495.     int in_shift_check = (out_bits <= xscale ? 8 - xscale : -1);
  496.     byte *d = dest;
  497.     uint h;
  498.  
  499.     for (h = height; h; srow += sskip, h -= yscale) {
  500.     const byte *s = srow;
  501.  
  502. #if ALPHA_LSB_FIRST
  503. #  define out_shift_initial 0
  504. #  define out_shift_update(out_shift, nbits) ((out_shift += (nbits)) >= 8)
  505. #else
  506. #  define out_shift_initial (8 - out_bits)
  507. #  define out_shift_update(out_shift, nbits) ((out_shift -= (nbits)) < 0)
  508. #endif
  509.     int out_shift = out_shift_initial;
  510.     byte out = 0;
  511.     int in_shift = in_shift_initial;
  512.     int dw = 8 - (srcx & 7);
  513.     int w;
  514.  
  515.     /* Loop over source bytes. */
  516.     for (w = width; w > 0; w -= dw, dw = 8) {
  517.         int index;
  518.         int in_shift_final =
  519.         (w >= dw ? 0 : dw - w);
  520.  
  521.         /*
  522.          * Check quickly for all-0s or all-1s, but only if each
  523.          * input byte generates no more than one output byte,
  524.          * we're at an input byte boundary, and we're processing
  525.          * an entire input byte (i.e., this isn't a final
  526.          * partial byte.)
  527.          */
  528.         if (in_shift == in_shift_check && in_shift_final == 0)
  529.         switch (*s) {
  530.             case 0:
  531.             for (index = sraster; index != sskip; index += sraster)
  532.                 if (s[index] != 0)
  533.                 goto p;
  534.             if (out_shift_update(out_shift, input_byte_out_bits))
  535.                 *d++ = out, out_shift &= 7, out = 0;
  536.             s++;
  537.             continue;
  538. #if !ALPHA_LSB_FIRST        /* too messy to make it work */
  539.             case 0xff:
  540.             for (index = sraster; index != sskip; index += sraster)
  541.                 if (s[index] != 0xff)
  542.                 goto p;
  543.             {
  544.                 int shift =
  545.                 (out_shift -= input_byte_out_bits) + out_bits;
  546.  
  547.                 if (shift > 0)
  548.                 out |= input_byte_out_mask << shift;
  549.                 else {
  550.                 out |= input_byte_out_mask >> -shift;
  551.                 *d++ = out;
  552.                 out_shift += 8;
  553.                 out = input_byte_out_mask << (8 + shift);
  554.                 }
  555.             }
  556.             s++;
  557.             continue;
  558. #endif
  559.             default:
  560.             ;
  561.         }
  562.       p:            /* Loop over source pixels within a byte. */
  563.         do {
  564.         uint count;
  565.  
  566.         for (index = 0, count = 0; index != sskip;
  567.              index += sraster
  568.             )
  569.             count += half_byte_1s[(s[index] >> in_shift) & mask];
  570.         if (count != 0 && table[count] == 0) {    /* Look at adjacent cells to help prevent */
  571.             /* dropouts. */
  572.             uint orig_count = count;
  573.             uint shifted_mask = mask << in_shift;
  574.             byte in;
  575.  
  576.             if_debug3('B', "[B]count(%d,%d)=%d\n",
  577.                   (width - w) / xscale,
  578.                   (height - h) / yscale, count);
  579.             if (yscale > 1) {    /* Look at the next "lower" cell. */
  580.             if (h < height && (in = s[0] & shifted_mask) != 0) {
  581.                 uint lower;
  582.  
  583.                 for (index = 0, lower = 0;
  584.                  -(index -= sraster) <= sskip &&
  585.                  (in &= s[index]) != 0;
  586.                 )
  587.                 lower += half_byte_1s[in >> in_shift];
  588.                 if_debug1('B', "[B]  lower adds %d\n",
  589.                       lower);
  590.                 if (lower <= orig_count)
  591.                 count += lower;
  592.             }
  593.             /* Look at the next "higher" cell. */
  594.             if (h > yscale && (in = s[sskip - sraster] & shifted_mask) != 0) {
  595.                 uint upper;
  596.  
  597.                 for (index = sskip, upper = 0;
  598.                  index < sskip << 1 &&
  599.                  (in &= s[index]) != 0;
  600.                  index += sraster
  601.                 )
  602.                 upper += half_byte_1s[in >> in_shift];
  603.                 if_debug1('B', "[B]  upper adds %d\n",
  604.                       upper);
  605.                 if (upper < orig_count)
  606.                 count += upper;
  607.             }
  608.             }
  609.             if (xscale > 1) {
  610.             uint mask1 = (mask << 1) + 1;
  611.  
  612.             /* Look at the next cell to the left. */
  613.             if (w < width) {
  614.                 int lshift = in_shift + xscale - 1;
  615.                 uint left;
  616.  
  617.                 for (index = 0, left = 0;
  618.                  index < sskip; index += sraster
  619.                 ) {
  620.                 uint bits =
  621.                 ((s[index - 1] << 8) +
  622.                  s[index]) >> lshift;
  623.  
  624.                 left += bits5_trailing_1s[bits & mask1];
  625.                 }
  626.                 if_debug1('B', "[B]  left adds %d\n",
  627.                       left);
  628.                 if (left < orig_count)
  629.                 count += left;
  630.             }
  631.             /* Look at the next cell to the right. */
  632.             if (w > xscale) {
  633.                 int rshift = in_shift - xscale + 8;
  634.                 uint right;
  635.  
  636.                 for (index = 0, right = 0;
  637.                  index < sskip; index += sraster
  638.                 ) {
  639.                 uint bits =
  640.                 ((s[index] << 8) +
  641.                  s[index + 1]) >> rshift;
  642.  
  643.                 right += bits5_leading_1s[(bits & mask1) << (4 - xscale)];
  644.                 }
  645.                 if_debug1('B', "[B]  right adds %d\n",
  646.                       right);
  647.                 if (right <= orig_count)
  648.                 count += right;
  649.             }
  650.             }
  651.             if (count > count_max)
  652.             count = count_max;
  653.         }
  654.         out += table[count] << out_shift;
  655.         if (out_shift_update(out_shift, out_bits))
  656.             *d++ = out, out_shift &= 7, out = 0;
  657.         }
  658.         while ((in_shift -= xscale) >= in_shift_final);
  659.         s++, in_shift += 8;
  660.     }
  661.     if (out_shift != out_shift_initial)
  662.         *d++ = out;
  663.     for (w = dskip; w != 0; w--)
  664.         *d++ = 0;
  665. #undef out_shift_initial
  666. #undef out_shift_update
  667.     }
  668. }
  669.  
  670. /* Extract a plane from a pixmap. */
  671. int
  672. bits_extract_plane(const bits_plane_t *dest /*write*/,
  673.     const bits_plane_t *source /*read*/, int shift, int width, int height)
  674. {
  675.     int source_depth = source->depth;
  676.     int source_bit = source->x * source_depth;
  677.     const byte *source_row = source->data.read + (source_bit >> 3);
  678.     int dest_depth = dest->depth;
  679.     uint plane_mask = (1 << dest_depth) - 1;
  680.     int dest_bit = dest->x * dest_depth;
  681.     byte *dest_row = dest->data.write + (dest_bit >> 3);
  682.     enum {
  683.     EXTRACT_SLOW = 0,
  684.     EXTRACT_4_TO_1,
  685.     EXTRACT_32_TO_8
  686.     } loop_case = EXTRACT_SLOW;
  687.     int y;
  688.  
  689.     source_bit &= 7;
  690.     dest_bit &= 7;
  691.     /* Check for the fast CMYK cases. */
  692.     if (!(source_bit | dest_bit)) {
  693.     switch (source_depth) {
  694.     case 4:
  695.         loop_case =
  696.         (dest_depth == 1 && !(source->raster & 3) &&
  697.          !(source->x & 1) ? EXTRACT_4_TO_1 :
  698.          EXTRACT_SLOW);
  699.         break;
  700.     case 32:
  701.         if (dest_depth == 8 && !(shift & 7)) {
  702.         loop_case = EXTRACT_32_TO_8;
  703.         source_row += 3 - (shift >> 3);
  704.         }
  705.         break;
  706.     }
  707.     }
  708.     for (y = 0; y < height;
  709.      ++y, source_row += source->raster, dest_row += dest->raster
  710.     ) {
  711.     int x;
  712.  
  713.     switch (loop_case) {
  714.     case EXTRACT_4_TO_1: {
  715.         const byte *source = source_row;
  716.         byte *dest = dest_row;
  717.  
  718.         /* Do groups of 8 pixels. */
  719.         for (x = width; x >= 8; source += 4, x -= 8) {
  720.         bits32 sword =
  721.             (*(const bits32 *)source >> shift) & 0x11111111;
  722.  
  723.         *dest++ =
  724.             byte_acegbdfh_to_abcdefgh[(
  725. #if arch_is_big_endian
  726.             (sword >> 21) | (sword >> 14) | (sword >> 7) | sword
  727. #else
  728.             (sword << 3) | (sword >> 6) | (sword >> 15) | (sword >> 24)
  729. #endif
  730.                     ) & 0xff];
  731.         }
  732.         if (x) {
  733.         /* Do the final 1-7 pixels. */
  734.         uint test = 0x10 << shift, store = 0x80;
  735.  
  736.         do {
  737.             *dest = (*source & test ? *dest | store : *dest & ~store);
  738.             if (test >= 0x10)
  739.             test >>= 4;
  740.             else
  741.             test <<= 4, ++source;
  742.             store >>= 1;
  743.         } while (--x > 0);
  744.         }
  745.         break;
  746.     }
  747.     case EXTRACT_32_TO_8: {
  748.         const byte *source = source_row;
  749.         byte *dest = dest_row;
  750.  
  751.         for (x = width; x > 0; source += 4, --x)
  752.         *dest++ = *source;
  753.         break;
  754.     }
  755.     default: {
  756.         sample_load_declare_setup(sptr, sbit, source_row, source_bit,
  757.                       source_depth);
  758.         sample_store_declare_setup(dptr, dbit, dbbyte, dest_row, dest_bit,
  759.                        dest_depth);
  760.  
  761.         sample_store_preload(dbbyte, dptr, dbit, dest_depth);
  762.         for (x = width; x > 0; --x) {
  763.         bits32 color;
  764.         uint pixel;
  765.  
  766.         sample_load_next32(color, sptr, sbit, source_depth);
  767.         pixel = (color >> shift) & plane_mask;
  768.         sample_store_next8(pixel, dptr, dbit, dest_depth, dbbyte);
  769.         }
  770.         sample_store_flush(dptr, dbit, dest_depth, dbbyte);
  771.     }
  772.     }
  773.     }
  774.     return 0;
  775. }
  776.  
  777. /* Expand a plane into a pixmap. */
  778. int
  779. bits_expand_plane(const bits_plane_t *dest /*write*/,
  780.     const bits_plane_t *source /*read*/, int shift, int width, int height)
  781. {
  782.     /*
  783.      * Eventually we will optimize this just like bits_extract_plane.
  784.      */
  785.     int source_depth = source->depth;
  786.     int source_bit = source->x * source_depth;
  787.     const byte *source_row = source->data.read + (source_bit >> 3);
  788.     int dest_depth = dest->depth;
  789.     int dest_bit = dest->x * dest_depth;
  790.     byte *dest_row = dest->data.write + (dest_bit >> 3);
  791.     enum {
  792.     EXPAND_SLOW = 0,
  793.     EXPAND_1_TO_4,
  794.     EXPAND_8_TO_32
  795.     } loop_case = EXPAND_SLOW;
  796.     int y;
  797.  
  798.     source_bit &= 7;
  799.     /* Check for the fast CMYK cases. */
  800.     if (!(source_bit || (dest_bit & 31) || (dest->raster & 3))) {
  801.     switch (dest_depth) {
  802.     case 4:
  803.         if (source_depth == 1)
  804.         loop_case = EXPAND_1_TO_4;
  805.         break;
  806.     case 32:
  807.         if (source_depth == 8 && !(shift & 7))
  808.         loop_case = EXPAND_8_TO_32;
  809.         break;
  810.     }
  811.     }
  812.     dest_bit &= 7;
  813.     switch (loop_case) {
  814.  
  815.     case EXPAND_8_TO_32: {
  816. #if arch_is_big_endian
  817. #  define word_shift (shift)
  818. #else
  819.     int word_shift = 24 - shift;
  820. #endif
  821.     for (y = 0; y < height;
  822.          ++y, source_row += source->raster, dest_row += dest->raster
  823.          ) {
  824.         int x;
  825.         const byte *source = source_row;
  826.         bits32 *dest = (bits32 *)dest_row;
  827.  
  828.         for (x = width; x > 0; --x)
  829.         *dest++ = (bits32)(*source++) << word_shift;
  830.     }
  831. #undef word_shift
  832.     }
  833.     break;
  834.  
  835.     case EXPAND_1_TO_4:
  836.     default:
  837.     for (y = 0; y < height;
  838.          ++y, source_row += source->raster, dest_row += dest->raster
  839.          ) {
  840.         int x;
  841.         sample_load_declare_setup(sptr, sbit, source_row, source_bit,
  842.                       source_depth);
  843.         sample_store_declare_setup(dptr, dbit, dbbyte, dest_row, dest_bit,
  844.                        dest_depth);
  845.  
  846.         sample_store_preload(dbbyte, dptr, dbit, dest_depth);
  847.         for (x = width; x > 0; --x) {
  848.         uint color;
  849.         bits32 pixel;
  850.  
  851.         sample_load_next8(color, sptr, sbit, source_depth);
  852.         pixel = color << shift;
  853.         sample_store_next32(pixel, dptr, dbit, dest_depth, dbbyte);
  854.         }
  855.         sample_store_flush(dptr, dbit, dest_depth, dbbyte);
  856.     }
  857.     break;
  858.  
  859.     }
  860.     return 0;
  861. }
  862.  
  863. /* ---------------- Byte-oriented operations ---------------- */
  864.  
  865. /* Fill a rectangle of bytes. */
  866. void
  867. bytes_fill_rectangle(byte * dest, uint raster,
  868.              byte value, int width_bytes, int height)
  869. {
  870.     while (height-- > 0) {
  871.     memset(dest, value, width_bytes);
  872.     dest += raster;
  873.     }
  874. }
  875.  
  876. /* Copy a rectangle of bytes. */
  877. void
  878. bytes_copy_rectangle(byte * dest, uint dest_raster,
  879.          const byte * src, uint src_raster, int width_bytes, int height)
  880. {
  881.     while (height-- > 0) {
  882.     memcpy(dest, src, width_bytes);
  883.     src += src_raster;
  884.     dest += dest_raster;
  885.     }
  886. }
  887.